/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panel;

import database.DataEngine;
import database.UserInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author daz
 */
public class UserInfoPanel extends Screen {

    private JLabel userNameLabel;
    private JTextField userNameInput;
    private JButton showInfoButton;
    private ActionListener showListenner;
    private DataEngine dataEngine = DataEngine.getInstance();
    private JLabel userInfoLabel;
    private JTextArea userInfoField;

    public UserInfoPanel(JFrame frame) {
        super(frame);
    }

    @Override
    void initValue() {
    }

    @Override
    void initComponent() {
        //cp line
        setLayout(null);
        int left = 10;
        int top = 20;
        int height = 20;
        int width = 250;
        int linePadding = 35;
        userNameLabel = new JLabel("User Name :");
        userNameLabel.setBounds(left + 30, top, 250, height);
        add(userNameLabel);
        userNameInput = new JTextField();
        userNameInput.setBounds(left + 130, top, width, height);
        add(userNameInput);
        // database user line
        top += linePadding;
        userInfoLabel = new JLabel("Thông tin:");
        userInfoLabel.setBounds(left + 30, top, 250, height);
        add(userInfoLabel);

        userInfoField = new JTextArea();
        userInfoField.setBounds(left + 130, top, width, 300);
        add(userInfoField);

        top += linePadding;
        showInfoButton = new JButton("Xem thông tin");
        add(showInfoButton);
        showInfoButton.addActionListener(showListenner);
        showInfoButton.setBounds(left, top, 100, height);
    }

    @Override
    void initListenner() {
        showListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (userNameInput.getText().length() == 0) {
                    showInfo("Chưa điền tên user name");
                    return;
                }
                UserInfo userInfo = dataEngine.getUserCp(userNameInput.getText());
                if (userInfo != null) {
                    userInfoField.setText("");
                    userInfoField.append("user name :" + userInfo.getUserName() + "\n");
                    userInfoField.append("Số điện thoại :" + userInfo.getPhoneNumber() + "\n");
                    userInfoField.append("cp name :" + userInfo.getCompanyName() + "\n");
                    userInfoField.append("cp id :" + userInfo.getCompanyId() + "\n");
                } else {
                    showInfo("user " + userNameInput.getText() + " không tồn tại");
                }
            }
        };
    }

    private void showInfo(String info) {
        JOptionPane.showMessageDialog(frame,
                info);
    }
}
